<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加处理页面</title>
</head>
<body>
<h2>添加处理页面</h2>
<%
    request.setCharacterEncoding("utf-8");
    int id= Integer.parseInt(request.getParameter("id"));
    String name=request.getParameter("name");
    String sex=request.getParameter("sex");
    int age= Integer.parseInt(request.getParameter("age"));
    float weight= Float.parseFloat(request.getParameter("weight"));
    float height= Float.parseFloat(request.getParameter("height"));
    System.out.println(id+name+sex+age+weight+height);

    String username="root";
    String pwd="pengfei";
    String driverName="com.mysql.jdbc.Driver";
    String dbName="student";
    String url="jdbc:mysql://127.0.0.1:3306/"+dbName+"?user="+username+"&password="+pwd+"&useUnicode=true&characterEncoding=UTF-8";
    String sql="INSERT into stu_info values(?,?,?,?,?,?);";
    Connection connection;

    try {
        Class.forName(driverName);
        System.out.println("驱动成功");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        connection = DriverManager.getConnection(url);
        System.out.println("连接成功");
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        preparedStatement.setString(3,sex);
        preparedStatement.setInt(4,age);
        preparedStatement.setFloat(5,weight);
        preparedStatement.setFloat(6,height);

        int flag=preparedStatement.executeUpdate();
        System.out.println(flag);
        if(flag>=1)
            out.print("插入成功！");
        else
            out.print("插入失败！");
        if (preparedStatement!=null)
            preparedStatement.close();
        if(connection!=null)
            connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

%>
</body>
</html>
